package com.annhoa.util.tag.thymeleaf;

import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractElementTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.templatemode.TemplateMode;

import com.annhoa.core.spring.SpringContextAware;
import com.annhoa.system.domain.User;
import com.annhoa.system.service.UserService;

public class DictMyTagProcessor extends AbstractElementTagProcessor {

	@Override
	protected void doProcess(ITemplateContext context, IProcessableElementTag tag,
			IElementTagStructureHandler handler) {
		String tagName = tag.getAttribute(ThyConst.ATTRIBUTE1).getValue();
		String clas = tag.getAttribute(ThyConst.ATTRIBUTE2).getValue();
		//初始访问无法获取上下文?????????????????
		// 通过获取上下文的方式获取service 无法使用spring 注入
		UserService userService = (UserService) SpringContextAware.getApplicationContext().getBean("userService");
		User user = (User) userService.findById(1);
		//List<User> userList = userService.findAll();
		String tagValue = "<" + tagName + " class=\""+clas+"\">" + user.getName() + "</" + tagName + ">";
		handler.replaceWith(tagValue, false);
	}

	public DictMyTagProcessor(String dialectPrefix) {
		super(TemplateMode.HTML, dialectPrefix, ThyConst.TAGNAME1, true, null, false, ThyConst.PRECEDENCE);
	}
}